package tool;

import java.io.File;

import cn.hxsteel.jtool.util.FileUtil;

/**
 * 验证类生成工具
 */
public class ValidClassGeneratorTool
{
    /**
     * 按模块名称创建验证类
     * 
     * @param moduleName 模块名称
     */
    public static void createByModuleName( String moduleName )
    {
	String userDir = System.getProperty( "user.dir" );
	String rootJavaDir = userDir + "\\src\\main\\java";
	String rootHBToolDir = userDir + "\\src\\main\\hbTool";
	String poDir = String.format( "%1$s\\cn\\hxsteel\\%2$s\\model\\po", rootJavaDir, moduleName );
	String validSrcPath = String.format( "%1$s\\%2$s\\valid.ctl", rootHBToolDir, moduleName );
	String validSrcContent = FileUtil.readFileToString( validSrcPath );
	String fileName = "";
	String objName = "";
	File config = new File( poDir );
	File temp[];
	temp = config.listFiles();
	for ( int i = 0; i < temp.length; i++ )
	{
	    fileName = temp[i].getName();
	    System.out.println( fileName );
	    System.out.println( temp[i] );
	    if ( fileName.contains( ".hbm.xml" ) )
	    {
		objName = fileName.replace( ".hbm.xml", "" );
		if ( objName.indexOf( "br_" ) == 0 )
		{
		    objName = objName.replace( "br_", "" );
		    String[] objNameArray = objName.split( "_" );
		    objName = objNameArray[0] + "Ref" + objNameArray[1].substring( 0, 1 ).toUpperCase() + objNameArray[1].substring( 1 );
		}
		else if ( objName.indexOf( "b_" ) == 0 )
		{
		    objName = objName.replace( "b_", "" );
		}
		objName = objName.substring( 0, 1 ).toUpperCase() + objName.substring( 1 );
		String validDestPath = String.format( "%1$s\\valid\\I%2$sGroup.java", poDir, objName );
		System.out.println( validDestPath );
		String data = String.format( validSrcContent, objName );
		FileUtil.writeStringToFile( validDestPath, data );
	    }
	}
    }
}